import CryptoJS from 'crypto-js'


function getUrlKey(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
}

/**
 * 时间转换
 */
function formatDate(dates) {
  let date = new Date(dates);
  let YY = date.getFullYear() + '-';
  let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  return YY + MM + DD ;
}
function formatMonth(dates) {
    let date = new Date(dates);
    let YY = date.getFullYear() + '-';
    let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) ;

    return YY + MM ;
}
/**
 * aes加密（AES/CBC/PKCS7Padding)
 * @param word
 * @returns {*}
 */
function encrypt(word) {
  var key = CryptoJS.enc.Utf8.parse("boncshzqpt202011");
  var srcs = CryptoJS.enc.Utf8.parse(word);
  var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
  return encrypted.toString();
}

/**
 * aes解密
 * @param word
 * @returns {*}
 */
function decrypt(word) {
  var key = CryptoJS.enc.Utf8.parse("boncshzqpt202011");
  var decrypt = CryptoJS.AES.decrypt(word, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

//开源工单中心
 function randomString(len) {
  len = len || 32;
  const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  const maxPos = $chars.length;
  let pwd = '';
  for (let i = 0; i < len; i++) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return pwd
}

/* 评论时间转化 */
function formatTime(time) {
  if (!time) return;
  if (time < 60) {
    time = `${Math.ceil(time)}秒`;
  } else if (time < 3600) {
    time = `${Math.ceil(time / 60)}分钟`;
  } else if (time < 3600 * 24) {
    time = `${Math.ceil(time / 60 / 60)}小时`;
  } else {
    time = `${Math.ceil(time / 60 / 60 / 24)}天`;
  }
  return time;
}

//事件防止抖动
function debounce(fn, wait) {
    if (this.timer !== null) {
        clearTimeout(this.timer)
    }
    this.timer = setTimeout(fn, wait)
}
//获取日期区间天数
function  getDaysBetween(dateString1,dateString2){
    var  startDate = Date.parse(dateString1);
    var  endDate = Date.parse(dateString2);
    var days=(endDate - startDate)/(1*24*60*60*1000);
    // alert(days);
    return  days;
}

export {encrypt, decrypt, getUrlKey, formatDate,randomString, formatTime,formatMonth,debounce,getDaysBetween}
